package com.eric.similarity.pipe;


import java.net.UnknownHostException;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.eric.similarity.Main.Config;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoPipe<K> implements Pipe<K> {
	Mongo mongo;
	DB db;
	DBCollection coll;
	
	boolean text;
	
	HashMap<Entry<K,K>,Entry<K,K>> MongoMap = new HashMap<Entry<K,K>,Entry<K,K>>();

	public MongoPipe (String host) throws UnknownHostException, MongoException {
		this.mongo = new Mongo(host);
	}
	
	public MongoPipe (String host,String db,String collection) throws UnknownHostException, MongoException {
		this.mongo = new Mongo(host);
		this.db = mongo.getDB(db);
		this.coll = this.db.getCollection(collection);
		MapOfPipe();
	}
	
	public MongoPipe () throws UnknownHostException, MongoException {
		this.mongo = new Mongo();
	}
	
	public MongoPipe (Mongo mongo,DB db,DBCollection coll) {
		this.mongo = mongo;
		this.db = db;
		this.coll = coll;
		MapOfPipe();
	}
	
	public void run () {
		MapOfPipe();
	}
	
	public void setCollection (String coll) {
		this.coll = db.getCollection(coll);
	}
	
	public void setDB (String db) {
		this.db = mongo.getDB(db);
	}
	
	public void text () {
		this.text = true;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void MapOfPipe() {
		// TODO Auto-generated method stub
		DBCursor cursor1 = coll.find();
		DBCursor cursor2 = cursor1.copy();
		Document doc1;
		Document doc2;
		
		while (cursor1.hasNext()) {
			DBObject entry1 = cursor1.next();

			if (entry1.get(Config.DOC_NAME) != null && entry1.get(Config.DOC_CONTENT) != null) {
				while (cursor2.hasNext()) {
					DBObject entry2 = cursor2.next();
							
					if (!entry2.equals(entry1)) {
						if (text) {
							doc1 = Jsoup.parse(entry1.get(Config.DOC_CONTENT).toString());
							doc2 = Jsoup.parse(entry2.get(Config.DOC_CONTENT).toString());
							
							MongoMap.put(
									new AbstractMap.SimpleEntry<K,K>(
									(K)
										entry1.get(Config.DOC_NAME).toString(), 
									(K)
										entry2.get(Config.DOC_NAME).toString()
									),					
									new AbstractMap.SimpleEntry<K,K>(
									(K)
										doc1.text(), 
									(K) 
										doc2.text())
									);
							
						}else{
							MongoMap.put(
									new AbstractMap.SimpleEntry<K,K>(
									(K)
										entry1.get(Config.DOC_NAME).toString(), 
									(K)
										entry2.get(Config.DOC_NAME).toString()
									),					
									new AbstractMap.SimpleEntry<K,K>(
									(K)
										entry1.get(Config.DOC_CONTENT).toString(), 
									(K) 
										entry2.get(Config.DOC_CONTENT).toString())
									);
						}
					}
				}
			}
			cursor2 = cursor1.copy();
		}
	}

	@Override
	public HashMap<Entry<K, K>, Entry<K, K>> getMap() {
		// TODO Auto-generated method stub
		return MongoMap;
	}

	@Override
	public Pipe<K> getPipe() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void write(Map<K, K> SimMap) {
		// TODO Auto-generated method stub
		
	}

}
